﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.User
{
    public class Group
    {
        protected void clsMenusInsert(tbl_Menu menuss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Menu menus = new tbl_Menu();
                    menus.MenuItemName = menuss.MenuItemName;
                    menus.MenuItemDescription = menuss.MenuItemDescription;
                    menus.Link = menuss.Link;
                    menus.MenuParentNo = menuss.MenuParentNo;
                    menus.IndexShow = menuss.IndexShow;
                    menus.CreatedDate = DateTime.Now;
                    menus.CreatedUser = menuss.CreatedUser;
                    menus.Status = menuss.Status;

                    adlerContextDb.tbl_Menus.InsertOnSubmit(menus);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsMenusModify(tbl_Menu menuss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var menus = adlerContextDb.tbl_Menus.Single(d => d.MenuNo == menuss.MenuNo);
                    menus.MenuItemName = menuss.MenuItemName;
                    menus.MenuItemDescription = menuss.MenuItemDescription;
                    menus.Link = menuss.Link;
                    menus.MenuParentNo = menuss.MenuParentNo;
                    menus.IndexShow = menuss.IndexShow;
                    menus.Status = menuss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsMenusDelete(tbl_Menu menuss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var menus = adlerContextDb.tbl_Menus.Single(d => d.MenuNo == menuss.MenuNo);
                    menus.Status = menuss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Menu.
        //public List<MODEL_SP_ADMIN_SEARCH_MENU> Get_Admin_Menu(string menuitemname, string status)
        //{
        //    using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
        //    {
        //        int index = 1;
        //        var query = from n in contextdb.SP_ADMIN_SEARCH_MENU(menuitemname, status)
        //                    select new MODEL_SP_ADMIN_SEARCH_MENU
        //                    {
        //                        Index = index++,
        //                        MenuNo = n.MenuNo,
        //                        MenuItemName = n.MenuItemName,
        //                        MenuItemDescription = n.MenuItemDescription,
        //                        Link = n.Link,
        //                        MenuParentNo = n.MenuParentNo,
        //                        IndexShow = n.IndexShow,
        //                        CreatedDate = n.CreatedDate,
        //                        CreatedUser = n.CreatedUser,
        //                        Status = n.Status
        //                    };
        //        return query.ToList();
        //    }
        //}

        public tbl_Menu GetMenuByMenuno(int menuno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Menus.Single(d => d.MenuNo == menuno);
            }
        }
    }
}
